﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using AuchanScan.Models;
using AuchanScan.ViewModels;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Controls;

namespace AuchanScan.Views
{
    public partial class ScanProductPage : PhoneApplicationPage
    {
        // Constructor
        public Achat _selectedAchat;

        public ScanProductPage()
        {
            InitializeComponent();
            DataContext = ViewModelLocator.MainViewModelStatic;
            Loaded += ScanProductPage_Loaded;
            StoryboardHideChangeQuantity.Completed += StoryboardHideChangeQuantity_Completed;
        }

        private void ScanProductPage_Loaded(object sender, RoutedEventArgs e)
        {
            SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape;
        }

        private void StoryboardHideChangeQuantity_Completed(object sender, EventArgs e)
        {
            SystemTray.IsVisible = true;
            SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape;
            ChangeQuantity.Visibility = Visibility.Collapsed;
        }


        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            base.OnBackKeyPress(e);
            if (ChangeQuantity.Visibility == Visibility.Visible)
            {
                e.Cancel = true;
                StoryboardHideChangeQuantity.Begin();
            }
        }

        private void Add_Click(object sender, EventArgs e)
        {
            SupportedOrientations = SupportedPageOrientation.Portrait;

            ((MainViewModel)DataContext).ScanAnArticle();
        }

        private void Remove_Click(object sender, EventArgs e)
        {
            SupportedOrientations = SupportedPageOrientation.Portrait;
            ((MainViewModel)DataContext).ScanAnArticleForRemove();
        }

        private void Buy_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/EnCaissePage.xaml", UriKind.Relative));
        }


        private void Trophyize_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/TrophyPage.xaml?a=simulate", UriKind.Relative));
        }

        private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            if ((e.Orientation & PageOrientation.Landscape) != 0)
            {
                ApplicationBar.IsVisible = false;
                TRProgressBar.Value = ViewModelLocator.MainViewModelStatic.TRPercent;
                NbrElemInBasket.Text =
                    ViewModelLocator.MainViewModelStatic.Datas.Courses.Select((c) => c.Quantity).Sum().ToString();
                Cagnotte.Text =
                    ViewModelLocator.MainViewModelStatic.Datas.Courses.Select(
                        (c) => c.Product.Cagnotte * c.Quantity).Sum().ToString();
                LandscapeView.Visibility = Visibility.Visible;
                SystemTray.IsVisible = false;
            }
            else
            {
                LandscapeView.Visibility = Visibility.Collapsed;
                ApplicationBar.IsVisible = true;
                SystemTray.IsVisible = true;
            }
        }

        private void ChangeQuantity_Click(object sender, RoutedEventArgs e)
        {
            ChangeQuantity.Visibility = Visibility.Visible;

            SupportedOrientations = SupportedPageOrientation.Portrait;
            SystemTray.IsVisible = false;
            StoryboardShowChangeQuantity.Begin();
            _selectedAchat = ((FrameworkElement)sender).DataContext as Achat;
            NbrElemn.Text = _selectedAchat.Quantity.ToString();
            DownQuantityButton.IsEnabled = _selectedAchat.Quantity > 1;
        }

        private void ToBuyList_Click(object sender, RoutedEventArgs e)
        {
            var achat = ((MenuItem)sender).DataContext as Achat;

            var vm = ((MainViewModel)DataContext);
            vm.AddToTheToBuyList(achat.Product);
            vm.RemoveItem(achat);
        }

        private void RemoveItem_Click(object sender, RoutedEventArgs e)
        {
            if (_selectedAchat != null)
            {
                ((MainViewModel)DataContext).RemoveItem(_selectedAchat);
            }
            StoryboardHideChangeQuantity.Begin();
        }

        private void DownQuantity_Click(object sender, RoutedEventArgs e)
        {
            ((MainViewModel)DataContext).ChangeQuantity(_selectedAchat, -1);
            DownQuantityButton.IsEnabled = _selectedAchat.Quantity > 1;
            NbrElemn.Text = _selectedAchat.Quantity.ToString();
        }

        private void UpQuantity_Click(object sender, RoutedEventArgs e)
        {
            ((MainViewModel)DataContext).ChangeQuantity(_selectedAchat, 1);
            DownQuantityButton.IsEnabled = true;
            NbrElemn.Text = _selectedAchat.Quantity.ToString();
        }

        private void EmptyCart_Click(object sender, System.EventArgs e)
        {
            ((MainViewModel)DataContext).EmptyCard();
        }


        private void Nav_Product(object sender, EventArgs e)
        {
            ViewModelLocator.MainViewModelStatic.CurrentProduct = ((sender as Button).DataContext as Achat);
            this.NavigationService.Navigate(new Uri(string.Format("/Views/ProductPage.xaml?ID={0}", ((MainViewModel)DataContext).CurrentProduct.Product.Id), UriKind.Relative));
        }
    }
}